﻿using System;
using System.Collections.Generic;
using System.Text;

namespace JustMathCore
{
    public static class AddrTools
    {
        public interface IAddrConv
        {
            AddrType Type
            {
                get;
            }
            byte[] Addr2Hash(string addr);
            //void Addr2Hash(string addr, byte[] outdata, int outpos);
            string GetAddr(byte[] prikey);
            byte[] GetHashAddr(byte[] prikey);
            string GetWif(byte[] prikey);
            string Hash2Addr(byte[] hash);
        }
        public enum AddrType
        {
            ERROR,
            BTC,
            BCH,
            ETH,
            NEO,
            NEO3,
            ONT,
            NEB,//星云
            LTC,
            DOGE,
            TRON,
        }
        public static readonly IAddrConv btc = new AddrToolCoin.BTC();
        public static readonly IAddrConv eth = new AddrToolCoin.ETH();
        public static readonly IAddrConv neo = new AddrToolCoin.NEO();
        public static readonly IAddrConv neo3 = new AddrToolCoin.NEO3();
        public static readonly IAddrConv neb = new AddrToolCoin.NEB();
        public static readonly IAddrConv ltc = new AddrToolCoin.LTC();
        public static readonly IAddrConv doge = new AddrToolCoin.DOGE();
        public static readonly IAddrConv tron = new AddrToolCoin.TRON();
        public static IAddrConv GetConv(AddrType type)
        {
            switch (type)
            {
                case AddrType.BTC:
                    return btc;
                case AddrType.ETH:
                    return eth;
                case AddrType.NEO:
                    return neo;
                case AddrType.NEO3:
                    return neo3;
                case AddrType.NEB:
                    return neb;
                case AddrType.LTC:
                    return ltc;
                case AddrType.DOGE:
                    return doge;
                case AddrType.TRON:
                    return tron;
                default:
                    throw new Exception("not support.");
            }
        }

        [ThreadStatic]
        static NBitcoin.DataEncoders.Base58Encoder base58;
        public static byte[] base58Decode(string str)
        {
            if (base58 == null) base58 = new NBitcoin.DataEncoders.Base58Encoder();
            return base58.DecodeData(str);
        }
        public static string base58Encode(byte[] data)
        {
            if (base58 == null) base58 = new NBitcoin.DataEncoders.Base58Encoder();
            return base58.EncodeData(data, 0, data.Length);
        }
    }

}
